package org.frasermccrossan.toneitdown;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.media.AudioManager;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.Toast;

public class EditProfile extends Activity {

	private ProfileDatabaseHelper pdh;
	private Profile profile;
	private final int CONFIRM_DIALOG = 1;
	Button doneButton;
	Button deleteButton;
	
	private OnClickListener buttonListener = new OnClickListener() {
		@SuppressWarnings("deprecation")
		public void onClick(View v) {
			if (v == doneButton) {
				if (pdh.profileIsActive(profile._id)) {
					Notify.applyProfile(EditProfile.this);
				}
				EditProfile.this.finish();
			}
			else if (v == deleteButton) {
				if (pdh.profileIsActive(profile._id)) {
					Toast.makeText(EditProfile.this, R.string.cannot_delete_active, Toast.LENGTH_SHORT).show();
				}
				else {
					showDialog(CONFIRM_DIALOG);
				}
			}
		}
	};
	
	private TextWatcher nameWatcher = new TextWatcher() {
		public void afterTextChanged(Editable s) {
			// don't care
		}
		
		public void beforeTextChanged(CharSequence s, int start, int count, int after) {
			// don't care
		}
		
		public void onTextChanged(CharSequence s, int start, int before, int count) {
			pdh.setName(profile._id, s);
		}
	};
	
	private OnSeekBarChangeListener volumeListener = new OnSeekBarChangeListener() {
		public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
			// don't care
		}
		
		public void onStartTrackingTouch(SeekBar seekBar) {
			// don't care
		}
		
		public void onStopTrackingTouch(SeekBar seekBar) {
			pdh.setVolume(profile._id, seekBar.getProgress());
			if (pdh.isCurrent(profile._id)) {
				Profile curProfile = pdh.getProfileById(profile._id);
				if (curProfile != null) {
					curProfile.apply(EditProfile.this, true);
				}
			}
		}
	};
	
	private OnCheckedChangeListener vibrateListener = new OnCheckedChangeListener() {
		public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
			pdh.setVibrate(profile._id, isChecked);
		}
	};
	
	private void deleteProfile() {
		pdh.deleteProfile(profile._id);
		this.finish();
	}
	
    @Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {
        case CONFIRM_DIALOG:
        	AlertDialog.Builder builder = new AlertDialog.Builder(this);
        	String message = String.format(getString(R.string.delete_confirm), profile.name);
        	builder.setMessage(message)
        	       .setCancelable(false)
        	       .setPositiveButton(getString(android.R.string.yes), new DialogInterface.OnClickListener() {
        	           public void onClick(DialogInterface dialog, int id) {
        	                deleteProfile();
        	           }
        	       })
        	       .setNegativeButton(getString(android.R.string.no), new DialogInterface.OnClickListener() {
        	           public void onClick(DialogInterface dialog, int id) {
        	                dialog.cancel();
        	           }
        	       });
        	return builder.create();
        }
        return null;
    }    
		
	@Override
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.edit_profile);
        pdh = new ProfileDatabaseHelper(this);
        
        Intent intent = getIntent();
        long profileId = intent.getLongExtra(PickProfile.profileNameTag, -1);
        profile = pdh.getProfileById(profileId);

        EditText profileNameField = (EditText)findViewById(R.id.profile_name);
        profileNameField.setText(profile.name);
        profileNameField.addTextChangedListener(nameWatcher);

        SeekBar volumeBar = (SeekBar)findViewById(R.id.volume_bar);
        volumeBar.setProgress(profile.volume);
		AudioManager am = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
		volumeBar.setMax(am.getStreamMaxVolume(AudioManager.STREAM_RING));
		volumeBar.setOnSeekBarChangeListener(volumeListener);

        CheckBox vibrateCheckbox = (CheckBox)findViewById(R.id.vibrate_checkbox);
        vibrateCheckbox.setChecked(profile.vibrate > 0);
        vibrateCheckbox.setOnCheckedChangeListener(vibrateListener);
        
        doneButton = (Button)findViewById(R.id.done);
        doneButton.setOnClickListener(buttonListener);
        
        deleteButton = (Button)findViewById(R.id.delete);
        deleteButton.setOnClickListener(buttonListener);
        
	    super.onCreate(savedInstanceState);
	}
	
	@Override
	public void onDestroy() {
		pdh.close();
		super.onDestroy();
	}
	
}
